from rest_framework import serializers
from .models import Goods, GoodsCategory


# 同一个模型的多级嵌套处理需要几级就定义几个同类型的模型
class CategorySerializerSub(serializers.ModelSerializer):
    """
    商品类子类序列列
    """
    class Meta:
        model = GoodsCategory
        fields = ('id', 'name', 'logo', 'sub_cat', 'parent')


class GoodCategorySerializer(serializers.ModelSerializer):
    """
    商品分类序列化
    """
    sub_cat = CategorySerializerSub(many=True)

    class Meta:
        model = GoodsCategory
        fields = ('id', 'name', 'logo', 'sub_cat', 'parent')


class GoodSerializer(serializers.ModelSerializer):
    """
    商品序列化
    """
    category = GoodCategorySerializer()
    # shop = ShopSerializers(many=True)

    class Meta:
        model = Goods
        fields = ('id', 'name', 'picture', 'price', 'category', 'shop')



